//
//  ARVoteCell.m
//  AriseSamePro
//
//  Created by 包日守 on 15/11/2.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARVoteCell.h"
#import "ARChoices.h"
#import "ARChoicesButton.h"
#import "UIImageView+ARRessume.h"

@interface ARVoteCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *voteView;
@property (weak, nonatomic) IBOutlet UILabel *votesLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoHightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *voteViewHightConstraint;

@property (nonatomic) NSMutableArray *marrayOfBttons;

@property (nonatomic) NSArray *voteColors;

@end

@implementation ARVoteCell


- (void) setCateModel:(ARCate5Model *)cateModel
{
    self.voteColors = @[[UIColor orangeColor],[UIColor yellowColor],[UIColor lightGrayColor],[UIColor groupTableViewBackgroundColor]];
    
    self.marrayOfBttons = [NSMutableArray array];
    
    _cateModel = cateModel;
    self.iconImageView.layer.cornerRadius = 16;
    self.iconImageView.clipsToBounds = YES;
    
    self.photoImageView.image = nil;
    self.iconImageView.image = nil;
    for (UIView *view in self.voteView.subviews) {
        [view removeFromSuperview];
    }
    
    [self.iconImageView setImageWithURL:[NSURL URLWithString:cateModel.userModel.avatar]];
   // [self.photoImageView setImageWithURL:[NSURL URLWithString:cateModel.mediaModel.voteModel.photo]];
    [self.photoImageView ar_setImageWithURL:cateModel.mediaModel.voteModel.photo];
    
    self.nameLabel.text = cateModel.userModel.username;
    self.votesLabel.text = cateModel.mediaModel.voteModel.total.stringValue;
    self.contentLabel.text = cateModel.mediaModel.voteModel.title;
    
    if (cateModel.mediaModel.voteModel.photo == nil) {
        self.photoHightConstraint.constant = 0.0;
    }
    else
    {
        self.photoHightConstraint.constant = 320.0;
    }
    
    float hight = 0;
    int i = 0;
    for (ARChoices *choices in cateModel.mediaModel.voteModel.choices) {
        hight += 44.0;
       
        
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ARChoicesButton" owner:nil options:nil];
        ARChoicesButton *choiceButton = [array lastObject];
        choiceButton.frame = CGRectMake(0, 44 * i , [UIScreen mainScreen].bounds.size.width, 44);
        [self.voteView addSubview:choiceButton];
        [choiceButton.voteButton setTitle:choices.title forState:UIControlStateNormal];
        choiceButton.pointLabel.hidden = YES;
        choiceButton.voteButton.tag = i;
        choiceButton.votes = choices.votes;
        
        [choiceButton.voteButton addTarget:self action:@selector(voteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
       UIView *colorView = [[UIView alloc] init];
        [choiceButton addSubview:colorView];
        [choiceButton sendSubviewToBack:colorView];
        choiceButton.voteColorView = colorView;
        
        
        choiceButton.voteColorView.frame = CGRectMake(0, 0, 0, 44);
        
        choiceButton.voteColorView.backgroundColor = self.voteColors[i];
        [self.marrayOfBttons addObject:choiceButton];
         i ++;
    }
    self.voteViewHightConstraint.constant = hight;
    
    if (cateModel.mediaModel.voteModel.my_choice.intValue!=0) {
        [self initColorViewWithIndex:cateModel.mediaModel.voteModel.my_choice.intValue - 1];
    }

}

- (void) voteButtonClick:(UIButton *) sender
{
    
    self.cateModel.mediaModel.voteModel.my_choice = [NSNumber numberWithInteger:sender.tag+1];
    
    
    int total = self.cateModel.mediaModel.voteModel.total.intValue;
    total ++;
    self.cateModel.mediaModel.voteModel.total = [NSNumber numberWithInt:total];
    
    [self setVoteButtonsWithIndex:sender.tag];
    

}


- (void) initColorViewWithIndex:(NSInteger) index
{
    int total = self.cateModel.mediaModel.voteModel.total.intValue;
    
    self.votesLabel.text = [NSString stringWithFormat:@"%d",total];
    
    for (int i = 0; i < self.marrayOfBttons.count; i ++) {
        ARChoicesButton *choicesButton = self.marrayOfBttons[i];
        choicesButton.pointLabel.hidden = NO;
        int count = choicesButton.votes.intValue;
        if (i == index) {
            count ++;
            
            choicesButton.voteButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            
        }
        else
        {
            choicesButton.voteButton.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        
        [choicesButton.voteButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        choicesButton.voteButton.userInteractionEnabled = NO;
        float point = (float)count/total;
        choicesButton.pointLabel.text = [NSString stringWithFormat:@"%d%%",(int)(point * 100)];
        choicesButton.voteColorView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * point, 44);
        
        
    }
    
    
    


}

- (void) setVoteButtonsWithIndex:(NSInteger) index
{
    int total = self.cateModel.mediaModel.voteModel.total.intValue;
    
    self.votesLabel.text = [NSString stringWithFormat:@"%d",total];
    
    for (int i = 0; i < self.marrayOfBttons.count; i ++) {
        ARChoicesButton *choicesButton = self.marrayOfBttons[i];
        choicesButton.pointLabel.hidden = NO;
        int count = choicesButton.votes.intValue;
        if (i == index) {
            count ++;
            
            choicesButton.voteButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            
        }
        else
        {
            choicesButton.voteButton.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        
        [choicesButton.voteButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        choicesButton.voteButton.userInteractionEnabled = NO;
        float point = (float)count/total;
        choicesButton.pointLabel.text = [NSString stringWithFormat:@"%d%%",(int)(point * 100)];
        
        [UIView animateWithDuration:0.5 animations:^{
            choicesButton.voteColorView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * point, 44);
        }];
        
        
    }

}


- (void)awakeFromNib {
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
